package com.lmx.repoDomain.repository;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.persistence.service.DeviceService;
import com.lmx.repoDomain.tmodel.Camera;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

/**
 * 食物
 *
 * @author ytche
 * @date 2017年8月30日
 *
 */
@JsonApiResourceRepository(value = Camera.class)
@Component
public class CameraRepository extends BaseRepository<Camera> {

	@Autowired
	private DeviceService deviceService; 
	
	@PostConstruct
	public void init() {
		super.dslJpaService.setEntityClass(Camera.class);
	}

	@Override
	@JsonApiFindAll
	public Iterable<Camera> findAll(final QueryParams requestParams) {
		return super.findAll(requestParams);
	}

	@Override
	@JsonApiSave
	public Camera save(Camera model) {
		if (model.getId() == null) {
			model = super.save(model);
		} else {
			if (model.getDelStatus() == 1) {// 删除
				// 删除 验证 CustomerFoodPlan errorcode 8
				dslJpaService.update(model);
			} else {
				if (model.getOperateFlag() != null && ((CommonConstants.DEVICE_OPERATE_CODE.BIND.label().equals(model.getOperateFlag())
						|| (CommonConstants.DEVICE_OPERATE_CODE.REMOVE_BIND.label().equals(model.getOperateFlag()))))) {// 进行解绑或者绑定操作
					deviceService.operateDeviceByFlag(null,model);
				} else {// 修改device信息
				dslJpaService.update(model);
				}
			}
		}
		return model;
	}

}
